<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="queryParams"
        ref="queryForm"
        @submit.native.prevent
        :inline="true"
        v-show="showSearch"
        label-width="75px"
      >
        <el-form-item label="业务系统" prop="applicationSystem">
          <el-select
            v-model="queryParams.applicationSystem"
            class="custom-select"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option
              class="custom-select"
              v-for="(item, index) in businessCodeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
              <span class="selectList">
                <common-marquee-text :content="item.dictLabel">
                </common-marquee-text>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据库名称" prop="dbName">
          <common-input
            v-model="queryParams.dbName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
      </el-form>
    </common-search>
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-switch
          v-model="isMatchNums"
          @change="matchNumChange"
          active-color="#13ce66"
          inactive-color="#909399"
          active-text="仅含有敏感字段数据"
          inactive-text="所有数据"
        >
        </el-switch>
      </el-col>
    </el-row>
    <div slot="tools" :gutter="10" class="mb8" style="margin-top: 15px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          :loading="exportLoading"
          @click="handleExport"
          >导出
        </el-button>
      </el-col>
    </div>
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @sort-change="sortChange"
      :default-sort="defaultSort"
    >
      <el-table-column
        label="业务系统"
        align="left"
        prop="applicationSystem"
        fixed
        show-overflow-tooltip
      />
      <el-table-column
        label="数据库名称"
        align="left"
        prop="dbName"
        show-overflow-tooltip
      />
      <el-table-column label="用户数量/库数量" align="left" prop="dbNum" />
      <el-table-column label="表数量" align="left" prop="tableNum" />
      <el-table-column
        label="敏感表数量"
        align="left"
        prop="senstableNum"
        sortable="custom"
      />
      <el-table-column label="字段数量" align="left" prop="columnNum" />
      <el-table-column
        label="敏感字段数量"
        align="left"
        prop="senscolumnNum"
        sortable="custom"
      />
      <el-table-column
        label="重要数据数量"
        align="left"
        prop="ext1"
        sortable="custom"
      />
      <el-table-column
        label="核心数据数量"
        align="left"
        prop="ext2"
        sortable="custom"
      />
      <!-- <el-table-column label="操作" align="center" width="80" fixed="right">
        <template slot-scope="scope">
          <el-dropdown @command="moreTool(scope.row, $event)">
            <span
              class="el-dropdown-link"
              style="cursor: pointer; margin-left: 8px; font-size: 12px"
              :style="{ '--theme-color': systemInfo.theme }"
              v-hasPermi="['datascan:table:list', 'datascan:statistics:query']"
            >
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" class="size">
              <el-dropdown-item
                command="pass"
                v-hasPermi="['datascan:table:list']"
              >
                通过
              </el-dropdown-item>
              <el-dropdown-item
                command="showTable"
                v-hasPermi="['datascan:table:list']"
              >
                查看表
              </el-dropdown-item>
              <el-dropdown-item
                command="showHistory"
                v-hasPermi="['datascan:statistics:query']"
              >
                历史任务
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column> -->

      <el-table-column label="操作" align="left" width="160" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updatePass(scope.row.dbId)"
            :disabled="scope.row.isPass === 1"
            v-hasPermi="['datascan:statistics:process']"
            >通过
          </el-button>
          <el-button
            type="text"
            @click="showTable(scope.row.dbId, scope.row.dbName)"
            v-hasPermi="['datascan:table:list']"
            >查看表
          </el-button>
          <el-button
            type="text"
            @click="showHistory(scope.row.dbId)"
            v-hasPermi="['datascan:statistics:query']"
            >历史任务
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <common-info-dialog
      title="查看历史任务数据"
      is-fixed-height
      :visible.sync="isHistoryDialog"
      @cancel="isHistoryDialog = false"
      :infoLoading="infoLoading"
    >
      <common-table :data="historyList" class="dialog-column-list">
        <el-table-column label="扫描开始时间" align="left" prop="beginTime" />
        <el-table-column label="扫描结束时间" align="left" prop="endTime" />
        <el-table-column
          label="操作"
          align="left"
          class-name="small-padding fixed-width"
          width="80"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showHistoryReport(scope.row)"
              v-hasPermi="['datascan:executionhistory:querytask']"
              >查看报告
            </el-button>
          </template>
        </el-table-column>
      </common-table>
      <pagination
        v-show="historyTotal > 0"
        :total="historyTotal"
        :page.sync="historyQueryParams.pageNum"
        :limit.sync="historyQueryParams.pageSize"
        @pagination="getList"
      />
    </common-info-dialog>

    <template v-if="isReport">
      <report-history
        :report-data="historyParams"
        @close="closeReport"
      ></report-history>
    </template>
  </common-list>
</template>

<script>
import {
  queryStatisticsListByPage,
  queryExecutionPolicyPageList,
  updatePass,
} from "@/api/datascan/scanresults/statistics";
import { queryBusinessList } from "@/api/datascan/dbinfo/info";
import { formatDate } from "@/utils";
import BaseList from "@/mixins/BaseList";
import ReportHistory from "./history";
import { getSingleArray, getFullDept } from "@/utils/util";
import { mapGetters } from "vuex";

export default {
  name: "Statistics",
  mixins: [BaseList],
  components: {
    ReportHistory,
  },
  data() {
    return {
      exportLoading: false,
      infoLoading: false,
      // 遮罩层
      loading: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扫描结果统计信息表格数据
      list: [],

      isHistoryDialog: false,
      historyList: [],
      historyTotal: 0,
      historyQueryParams: {
        dbId: null,
        pageNum: 1,
        pageSize: 10,
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dbName: null,
        deptIds: null,
        matchNums: "0", // 是否包含敏感字符  所有数据时为空字符串 仅包含敏感词时为 0
        feildName: "senstableNum",
        order: "desc",
        applicationSystem: null,
      },
      historyParams: {
        taskId: 101,
        historyId: 112,
        dbId: 101,
      },
      isReport: false,
      currentParent: 4,
      singleDeptList: [],

      isMatchNums: true,
      defaultSort: {
        prop: "senstableNum",
        order: "descending",
      },
      businessCodeList: [],
    };
  },
  computed: {
    ...mapGetters(["systemInfo"]),
  },
  created() {
    this.getList();
    this.queryBusinessCodeList();
  },
  methods: {
    //获取业务系统列表
    queryBusinessCodeList() {
      queryBusinessList().then((res) => {
        if (res && res.code === 200) {
          this.businessCodeList = res.data;
        } else {
          this.businessCodeList = [];
        }
      });
    },
    sortChange(sortInfo) {
      let { column, order, prop } = sortInfo;
      this.queryParams.order =
        order === "descending" ? "desc" : order === "ascending" ? "asc" : null;

      this.queryParams.feildName = this.queryParams.order ? prop : null;

      this.defaultSort.prop = this.queryParams.feildName;
      this.defaultSort.order = order;
      this.getList();
    },

    moreTool(row, operate) {
      if (operate === "showTable") {
        this.showTable(row.dbId, row.dbName);
      } else if (operate === "showHistory") {
        this.showHistory(row.dbId);
      } else if (operate === "pass") {
        this.updatePass(row.dbId);
      }
    },
    updatePass(dbId) {
      let params = {
        isPass: 1,
        dbId,
      };
      updatePass(params).then((res) => {
        if (res && res.code === 200) {
          this.$GlobalSuccessMessage("操作成功");
          this.getList();
        } else {
          this.$GlobalErrorConfirm("操作失败", res.msg);
        }
      });
    },
    matchNumChange() {
      this.queryParams.matchNums = this.isMatchNums ? 0 : "";
      this.handleQuery();
    },
    getDeptName(val) {
      let dept = this.singleDeptList.find((item) => item.id === val.toString());
      if (dept) return getFullDept(dept.fulllabel);
    },
    //导出
    handleExport() {
      this.exportLoading = true;
      this.download(
        "datascan/b/scanresultstatistics/exportTemplate", //接口路径
        {
          ...this.queryParams,
          pageSize: 10000,
        },
        `扫描结果_库_${new Date().getTime()}.xlsx`
      ).finally(() => {
        this.exportLoading = false;
      });
    },
    /** 查询扫描结果统计信息列表 */
    getList() {
      this.loading = true;
      queryStatisticsListByPage(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.data.dataList;
            this.total = res.data.totalCount;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.historyQueryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      // this.$refs.commonDept.clearDept();
    },

    showTable(id, dbName) {
      this.$emit("change", "table", {
        dbId: id,
        dbName: dbName,
      });
    },
    showHistory(id) {
      this.historyParams.dbId = id;
      this.isHistoryDialog = true;
      this.detail(id);
    },
    detail(id) {
      this.infoLoading = true;
      queryExecutionPolicyPageList({
        ...this.historyQueryParams,
        dbId: id,
      })
        .then((res) => {
          if (res && res.code === 200) {
            this.historyList = res.data.dataList.reduce((r, c) => {
              c.beginTime = formatDate(c.beginTime);
              c.endTime = formatDate(c.endTime);
              r.push(c);
              return r;
            }, []);
            this.infoLoading = false;
            // this.total = res.data.totalCount
          } else {
            this.$GlobalErrorConfirm("获取信息失败", res.msg, "重新加载").then(
              () => {
                this.detail(id);
              }
            );
          }
        })
        .finally(() => {});
    },
    showHistoryReport(task) {
      this.historyParams.taskId = task.taskId;
      this.historyParams.historyId = task.historyId;

      /*this.$router.push({
        path: '/datascanservice/history',
        query: this.historyParams
      })*/
      this.isHistoryDialog = false;
      this.isReport = true;
    },
    closeReport() {
      this.isReport = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-dropdown-link {
  color: var(--theme-color);
}
</style>
